$(function () {
    //暂时注释
    var username=document.getElementById("get_username");
    username.innerText=UrlParam.paramValues("username")[0];
    var userid=document.getElementById("get_userid");
    userid.innerText=UrlParam.paramValues("userid")[0];

    findallhotvideo('');
    //默认
    $('#publish-hot').addClass("active");
    // $('#publish-btn').addClass("active");
    $('#hot-list').css({
        'display': 'block'
    });
    $('#publish-hot').click(function () {
        $('#publish-hot').addClass("active");
        $('#videolist-btn').removeClass("active");
        $('#publish-btn').removeClass("active");
        $('#video-list').css({
            'display': 'none'
        });
        $('#publish-video').css({
            'display': 'none'
        });
        $('#hot-list').css({
            'display': 'block'
        });
    });
    $('#publish-btn').click(function () {
        $('#publish-btn').addClass("active");
        $('#videolist-btn').removeClass("active");
        $('#publish-hot').removeClass("active");
        $('#video-list').css({
            'display': 'none'
        });
        $('#hot-list').css({
            'display': 'none'
        });
        $('#publish-video').css({
            'display': 'block'
        });
        
    });
    $('#videolist-btn').click(function () {
        findonehotvideo("");
        $('#videolist-btn').addClass("active");
        $('#publish-btn').removeClass("active");
        $('#publish-hot').removeClass("active");
        $('#publish-video').css({
            'display': 'none'
        });
        $('#hot-list').css({
            'display': 'none'
        });
        $('#video-list').css({
            'display': 'block'
        });

    });
});


function sendToUser(){ //在这里进行ajax 文件上传 用户的信息 
    var url="video/upload.do";
    var $file1 = $("input[name='fileName1']").val();//用户文件内容(文件)
    // 判断文件是否为空 
    if ($file1 == "") {
        alert("请选择上传的目标文件! ")
        return false;
    }
    //判断文件大小
    var size1 = $("input[name='fileName1']")[0].files[0].size;
    if (size1>1048576000) {
        alert("上传文件不能大于1G!");
        return false;				
    }
    var type = "file";
    var formData = new FormData();//这里需要实例化一个FormData来进行文件上传
    formData.append(type,$("#fileName1")[0].files[0]);
    formData.append("csrfmiddlewaretoken", "{{ csrf_token }}");
    // var userId=document.getElementById("get_userid").innerText;

    $.ajax({
            type : "post",
            url : url,
            data : formData,
            processData : false,
            contentType : false,
            success : function(data){
                if (data=="error") {
                    alert("文件提交失败!");
                }else{
                $("input[name='userUrl']").val(data);
                alert("文件上传成功!");
            }},
            error:function(){
                alert("连接网络失败!");
            }
            
        });
    }
function findallhotvideo(key){
console.log("findallhotvideo")
var table=document.getElementById("all_hotvideo");
var url='/SoftwareDesign/video/playvideo.do?key='+key;
$.ajax({
        type : "get",
        url : url,
        dataType : "json",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        success : function(data) {//根据返回的数据画出表格
            console.log(data);
            if(data!=null){
                var rows=table.rows;
                for(var i=rows.length-1;i>=0;i--){
                    rows[i].remove();
                }
                for(var i=0;i<data.picturesrc.length;i++){
                    console.log(data.videosrc.length)
                    
                    var row=document.createElement('tr');
                    row.innerHTML="<td class='.col-md-6' style='display: block;'><video height='320' width='180'  controls preload='metadata'><source src='"+data.videosrc[i]+"'></video></td>"
                    +"<td>"+data.videoname[i]+"</td><td class='.col-md-4'><button class='glyphicon glyphicon-heart'> 收藏 </button></td>";
                    console.log(data.videosrc[i]);
                    console.log(data.videoname[i]);
                    table.appendChild(row);
                }
            } 
        },
        error : function(){
            alert("连接服务器异常！")
        }
   })
}
function findonehotvideo(key){
    var table=document.getElementById("one_hotvideo");
    var url='/SoftwareDesign/video/playvideo.do?key='+key;
    $.ajax({
            type : "get",
            url : url,
            dataType : "json",
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            success : function(data) {//根据返回的数据画出表格
                if(data!=null){
                    var rows=table.rows;
                    for(var i=rows.length-1;i>=0;i--){
                        rows[i].remove();
                    }
                    for(var i=0;i<data.picturesrc.length;i++){
                        
                        console.log("picturesrc"+data.picturesrc.length);
                        var row=document.createElement('tr');
                        row.innerHTML="<td><img class='video-img'  height='320' width='180'  src='"+data.picturesrc[i]+"'>"
                        +"<td>"+data.videoname[i]+"</td><td ><button class='btn btn-warning'>删除</button><button class='btn btn-success'>分享</button></td>";
                        console.log("picturesrc"+data.picturesrc[i]);
                        console.log("picturesrc"+data.videoname[i]);
                        table.appendChild(row);
                    }
                } 
            },
            error : function(){
                alert("连接服务器异常！")
            }
       })
    }


